/*
 * 文件名：OrderByInfoAction.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：ozm
 * 修改时间：2013-1-29
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.facility.device.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.wootide.cms.common.Constants;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.Result;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.facility.device.pojo.DeviceInfo;
import com.wootide.cms.service.facility.device.service.DeviceInfoService;
import com.wootide.cms.service.facility.room.roominfo.pojo.RoomInfo;
import com.wootide.cms.sys.common.action.BaseAction;

/**
 * 预订方式控制层
 * 〈一句话功能简述〉
 * 〈功能详细描述〉
 * @author ozm
 * @version 2013-1-29
 * @see DeviceInfoAction
 * @since 2013-1-29下午1:33:34
 */
public class DeviceInfoAction extends BaseAction implements ServletRequestAware
{
    
    /**
     * 意义，目的和功能，以及被用到的地方<br>
     */
    private static final long serialVersionUID = 1L;
    
    //预订方式信息
    private DeviceInfo deviceInfo;
    
    //日志
    private RunLog runLog;
    
    //预订方式信息系业务层实例
    private DeviceInfoService deviceInfoService;
    
    //分页信息
    private Page page;
    
    //查询条件
    private QueryCondition queryConditon;
    
    // Http请求对象
    private HttpServletRequest request;
    
    // 预订方式结果集
    @SuppressWarnings("rawtypes")
    private List results;
    
    //返回消息
    private String message;
    
    // 是否操作成功
    private Integer actResult = Constants.ACTRESULT_DEF;
    
    // 预订方式ID
    private Integer innerIndex;
    
    //预订方式集合
    private Integer[] innerIndexs;
    
    /**
     * 
     * Description:分页查询
     * Implement: <br>
     * @return
     * @see
     * @return String 条转换标识
     * @author ozm2013-1-29下午1:53:19
     */
    public String doQuery()
    {
        try
        {
            //            return SUCCESS;
            // 如果从菜单进入，即在没有分页信息的情况下，则默认进入首页
            if (null == page)
            {
                page = new Page();
                page.setCurPage(1);
            }
            
            // 调用而业务层进行分页查询
            Result result = deviceInfoService.queryForPages(page, queryConditon);
            
            int resultCode = result.getResultCode();
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                QueryResults queryResults = (QueryResults)result.getResultPojo();
                // 获取服务代码结果集
                runLog.debug(queryResults);
                results = queryResults.getResults();
                // 获取分页信息
                page = queryResults.getPage();
                
                // 跳转到分页页面
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }
            
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 预添加
     * Implement: <br>
     * @see
     * @return String 跳转标识
     * @author ozm2013-1-29下午3:35:20
     */
    public String toAdd()
    {
        try
        {
            /**
             * 设置页面字段的默认值 默认值
             */
            deviceInfo = new DeviceInfo();
            return SUCCESS;
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
        
    }
    
    
    /**
     * Description: 预修改易耗品信息<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String toMod()
    {
        try
        {
            // 调用业务层获取一个路由对象
            Result result = deviceInfoService.viewDeviceInfo(innerIndex);
            int resultCode = result.getResultCode();
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                deviceInfo = (DeviceInfo)result.getResultPojo();
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    //    /**
    //     * 
    //     * Description: 预添加
    //     * Implement: <br>
    //     * @see
    //     * @return String 跳转标识
    //     * @author ozm2013-1-29下午3:35:20
    //     */
    //    public String toAdd()
    //    {
    //        try
    //        {
    //            
    //            // 调用业务层进行前转MO路由新增操作
    //            Result roomTypeInfoResult = roomTypeInfoService.queryAllRoomTypeInfos();
    //            
    //            int roomTypeInfoResultCode = roomTypeInfoResult.getResultCode();
    //            
    //            if (ErrorCodes.ERRORCODE_SUCC == roomTypeInfoResultCode)
    //            {
    //                
    //                QueryResults roomTypeInfoResults = (QueryResults)roomTypeInfoResult.getResultPojo();
    //                // 获取服务代码结果集
    //                runLog.debug(roomTypeInfoResults);
    //                
    //                roomTypeInfos = roomTypeInfoResults.getResults();
    //                
    //                return SUCCESS;
    //            }
    //            else
    //            {
    //                message = getText(Constants.MESSAGE_PREFIX
    //                        + roomTypeInfoResultCode);
    //                
    //                return ERROR;
    //            }
    //        }
    //        catch (Exception e)
    //        {
    //            
    //            runLog.error("Unknow Error!", e, request);
    //            actResult = Constants.ACTRESULT_FAIL;
    //            message = getText(Constants.MESSAGE_PREFIX
    //                    + ErrorCodes.ERRORCODE_UNKNOW);
    //            return ERROR;
    //        }
    //        
    //    }
    
    /**
     * Description: 删除易耗品信息<br>
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author wuliquan
     */
    public String doDel()
    {
        try
        {
            runLog.debug("doDel");
            // 调用业务层进行删除操作
            Result result = deviceInfoService.delDeviceInfo(innerIndexs,
                    request);
            int resultCode = result.getResultCode();
            
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            // 如果删除中出现失败
            else
            {
                // 取得删除失败的条数
                int delErrorNum = result.getDelErrorNum();
                // 根据总条数和失败条数得出成功的条数
                int delSuccNum = innerIndexs.length - delErrorNum;
                // 定义国际化消息的参数
                String[] params = new String[] {String.valueOf(delSuccNum),
                        String.valueOf(delErrorNum)};
                message = getText(Constants.MESSAGE_PREFIX + resultCode, params);
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    
    /**
     * 
     * Description: 添加易耗品信息<br>
     * Implement: <br>
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String doAdd()
    {
        try
        {
            runLog.debug("deviceInfo=" + deviceInfo);
            
            // 调用业务层进行前转MO路由新增操作
            Result result = deviceInfoService.addDeviceInfo(deviceInfo,
                    request);
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 获取系统HttpServletRequest实例
     */
    public void setServletRequest(HttpServletRequest request)
    {
        
        this.request = request;
    }
    
    public RunLog getRunLog()
    {
        return runLog;
    }
    
    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }
    
    public DeviceInfoService getDeviceInfoService()
    {
        return deviceInfoService;
    }
    
    public void setDeviceInfoService(DeviceInfoService deviceInfoService)
    {
        this.deviceInfoService = deviceInfoService;
    }
    
    public Page getPage()
    {
        return page;
    }
    
    public void setPage(Page page)
    {
        this.page = page;
    }
    
    public QueryCondition getQueryConditon()
    {
        return queryConditon;
    }
    
    public void setQueryConditon(QueryCondition queryConditon)
    {
        this.queryConditon = queryConditon;
    }
    
    public HttpServletRequest getRequest()
    {
        return request;
    }
    
    public void setRequest(HttpServletRequest request)
    {
        this.request = request;
    }
    
    @SuppressWarnings("rawtypes")
    public List getResults()
    {
        return results;
    }
    
    @SuppressWarnings("rawtypes")
    public void setResults(List results)
    {
        this.results = results;
    }
    
    public String getMessage()
    {
        return message;
    }
    
    public void setMessage(String message)
    {
        this.message = message;
    }
    
    public Integer getActResult()
    {
        return actResult;
    }
    
    public void setActResult(Integer actResult)
    {
        this.actResult = actResult;
    }
    
    public Integer getInnerIndex()
    {
        return innerIndex;
    }
    
    public void setInnerIndex(Integer innerIndex)
    {
        this.innerIndex = innerIndex;
    }
    
    public Integer[] getInnerIndexs()
    {
        return innerIndexs;
    }
    
    public void setInnerIndexs(Integer[] innerIndexs)
    {
        this.innerIndexs = innerIndexs;
    }

    public DeviceInfo getDeviceInfo()
    {
        return deviceInfo;
    }

    public void setDeviceInfo(DeviceInfo deviceInfo)
    {
        this.deviceInfo = deviceInfo;
    }
    
}
